package jmcnet.libcommun.communs
{
	import flexunit.framework.TestCase;
	import flexunit.framework.TestSuite;
	import jmcnet.libcommun.communs.time.Time;
	import jmcnet.libcommun.communs.time.TimeHHMMSS;

	public class TimeTest extends TestCase
	{
		public function TimeTest(methodName:String=null)
		{
			super(methodName);
		}
		
		public static function suite():TestSuite {
   			var ts:TestSuite = new TestSuite();
   			
   			ts.addTest( new TimeTest( "testTimeToString" ) );
   			ts.addTest( new TimeTest( "testTimeNormalize" ) );
   			ts.addTest( new TimeTest( "testAddTime" ) );
   			ts.addTest( new TimeTest( "testSubTime" ) );
   			ts.addTest( new TimeTest( "testStringToTime" ) );
   			ts.addTest( new TimeTest( "testTimeHHMMSS" ) );
   			// ts.addTest( new TemperatureConverterTest( "testToCelsius" ) );
   			return ts;
   		}
   		
   		public function testTimeToString():void {
   			var t:Time = new Time(12, 34, 48, 512);
   			var s:String = t.timeToString(true, true, true, true);
   			trace (s);
   			assertEquals("12:34:48.512", s);
   			
   			s = t.timeToString(true, true, true, false);
   			trace (s);
   			assertEquals("12:34:48", s);
   			
   			s = t.timeToString(true, true, false, false);
   			trace (s);
   			assertEquals("12:34", s);
   			
   			s = t.timeToString(true, false, false, false);
   			trace (s);
   			assertEquals("12", s);
   			
   			s = t.timeToString(false, false, false, false);
   			trace (s);
   			assertEquals("", s);
   			
   			s = t.timeToString(false, true, false, true);
   			trace (s);
   			assertEquals("34.512", s);
   			
   			s = t.timeToString(true, false, true, false);
   			trace (s);
   			assertEquals("12:48", s);
   			
   			s = t.timeToString(true, false, false, true);
   			trace (s);
   			assertEquals("12.512", s);
   			
   			t.setTime(2, 1, 3, 4);
   			assertEquals("02:01:03.004", t.timeToString(true, true, true, true));
   			
   			t.setTime(2,1,3,40);
   			assertEquals("02:01:03.040", t.timeToString(true, true, true, true));
   		}
   		
   		public function testTimeNormalize():void {
   			var t:Time = new Time(12, 34, 48, 1024);
   			assertEquals("12:34:49.024", t.timeToString(true, true, true, true));
   			
   			t.setTime(12,34,66,789);
   			assertEquals("12:35:06.789", t.timeToString(true, true, true, true));
   			
   			t.setTime(12,64,06,789);
   			assertEquals("13:04:06.789", t.timeToString(true, true, true, true));
   		}
   		
   		public function testAddTime():void {
   			var t:Time = new Time(12, 34, 48, 24);
   			var debordement:Boolean = t.addTime(new Time(2, 3, 4, 5));
   			assertFalse(debordement);
   			assertEquals("14:37:52.029", t.timeToString(true, true, true, true));
   			
   			debordement = t.addTime(new Time(0,0,0,2000));
   			assertFalse(debordement);
   			assertEquals("14:37:54.029", t.timeToString(true, true, true, true));
   			
   			debordement = t.addTime(new Time(0,0,70,0));
   			assertFalse(debordement);
   			assertEquals("14:39:04.029", t.timeToString(true, true, true, true));
   			
   			debordement = t.addTime(new Time(0,30,0,0));
   			assertFalse(debordement);
   			assertEquals("15:09:04.029", t.timeToString(true, true, true, true));
   			
   			debordement = t.addTime(new Time(10,0,0,0));
   			assertTrue(debordement);
   			assertEquals("01:09:04.029", t.timeToString(true, true, true, true));
   		}
   		
   		public function testSubTime():void {
   			var t:Time = new Time(12, 34, 48, 24);
   			var debordement:Boolean = t.subTime(new Time(2, 3, 4, 5));
   			assertFalse(debordement);
   			assertEquals("10:31:44.019", t.timeToString(true, true, true, true));
   			
   			debordement = t.subTime(new Time(0, 0, 0, 29));
   			assertFalse(debordement);
   			assertEquals("10:31:43.990", t.timeToString(true, true, true, true));
   			
   			debordement = t.subTime(new Time(0, 0, 73, 0));
   			assertFalse(debordement);
   			assertEquals("10:30:30.990", t.timeToString(true, true, true, true));
   			
   			debordement = t.subTime(new Time(0, 45, 0, 0));
   			assertFalse(debordement);
   			assertEquals("09:45:30.990", t.timeToString(true, true, true, true));
   			
   			debordement = t.subTime(new Time(10, 0, 0, 0));
   			assertTrue(debordement);
   			assertEquals("23:45:30.990", t.timeToString(true, true, true, true));
   			
   			// Le test qui tue
   			t.setTime(0, 0, 0, 0);
   			debordement = t.subTime(new Time(0, 0, 0, 1));
   			assertEquals("23:59:59.999", t.timeToString(true, true, true, true));
   			assertTrue(debordement);
   			
   		}
   		
   		public function testStringToTime():void {
   			var t:Time = Time.stringToTime("01:02:03.254", true, true, true, true);
   			assertEquals(t.hour, 1);
   			assertEquals(t.minute, 2);
   			assertEquals(t.second, 3);
   			assertEquals(t.millisecond, 254);
   			
   			t = Time.stringToTime("02:03.254", false, true, true, true);
   			assertEquals(t.hour, 0);
   			assertEquals(t.minute, 2);
   			assertEquals(t.second, 3);
   			assertEquals(t.millisecond, 254);
   			
   			t = Time.stringToTime("03.254", false, false, true, true);
   			assertEquals(t.hour, 0);
   			assertEquals(t.minute, 0);
   			assertEquals(t.second, 3);
   			assertEquals(t.millisecond, 254);
   			
   			t = Time.stringToTime("254", false, false, false, true);
   			assertEquals(t.hour, 0);
   			assertEquals(t.minute, 0);
   			assertEquals(t.second, 0);
   			assertEquals(t.millisecond, 254);
   			
   			// Erreur sur les heures
   			try {
	   			t = Time.stringToTime("02:03.254", true, true, true, true);
	   			fail("On en devrait pas pouvoir extraire car manque les heures");
	   		} catch (err:Error) {
	   			// OK
	   		}
	   		
	   		// Erreur sur les minutes
   			try {
	   			t = Time.stringToTime("01:03.254", true, true, true, true);
	   			fail("On en devrait pas pouvoir extraire car manque les minutes");
	   		} catch (err:Error) {
	   			// OK
	   		}
	   		
	   		// Erreur sur les secondes
   			try {
	   			t = Time.stringToTime("01.254", true, false, true, true);
	   			fail("On en devrait pas pouvoir extraire car manque les secondes");
	   		} catch (err:Error) {
	   			// OK
	   		}
	   		
	   		// Erreur sur les millisecondes
   			try {
	   			t = Time.stringToTime("01:02:03", true, true, true, true);
	   			fail("On en devrait pas pouvoir extraire car manque les millisecondes");
	   		} catch (err:Error) {
	   			// OK
	   		}
   		}
   		
   		public function testTimeHHMMSS():void {
   			var t:TimeHHMMSS = TimeHHMMSS.stringToTimeHHMMSS("01:02:03.254", true, true, true);
   			assertEquals("01:02:03", t.toString());

   		}
		
	}
}